function S = selectStatements(Q, SS)

%Takes two structs and returns the statement query
%Q: has key and url values to access the deployment
%SS: has the S3QL allowed values to construct the query.
%For example SS fields can be:
%   SS.item_id = 2333
%   SS.rule_id = 844585
%   SS.statement_id = 89955
%The fieldnames should be S3QL compliant as these will be used to construct
%the query. Also, SS struct should only contain the fieldnames that will
%have a value in the query. 

%ROmesh Stanislaus May 2009

%check to see if SS is a struct
if not(isstruct(SS))
    error('The second arg should be a struct...see help')
end

%get filednames
fNames = fieldnames(SS);

%generate query body
body='';
for i = 1:size(fNames,1)
    try
        body = [body,'<',fNames(i),'>',num2str(getfield(SS,fNames{i})),'</',fNames(i),'>'];
    catch
        body = [body,'<',fNames(i),'>',getfield(SS,fNames{i}),'</',fNames(i),'>'];
    end
    %body = [body,b1];
end
%combine the cell contents together
body=[body{1,:}];

%construct S3QL query
front = '<S3QL><select>*</select><from>statements</from><where>';
back = '</where></S3QL>';

S1 = [front,body,back];

Q.X = S1;

[D1,Q]=s3db_read(Q); % -2
%D1 will be a struct which has among other item_id wich can be referenced
%by D1(1).item_id
%pause(3);
S = D1;